package com.itwxz.forum.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.houbb.sensitive.word.core.SensitiveWordHelper;
import com.itwxz.common.utils.ip.AddressUtils;
import com.itwxz.common.utils.ip.IpUtils;
import com.itwxz.forum.domain.po.ForumPost;
import com.itwxz.forum.domain.po.ForumUser;
import com.itwxz.forum.domain.vo.ForumPostListVo;
import com.itwxz.forum.domain.vo.ForumPostVo;
import com.itwxz.forum.enums.PostHasImage;
import com.itwxz.forum.mapper.ForumPostMapper;
import com.itwxz.forum.service.ForumPostService;
import com.itwxz.forum.service.ForumUserService;
import com.itwxz.forum.utils.SaUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Admin
 * @description 针对表【forum_post(论坛帖子表)】的数据库操作Service实现
 * @createDate 2025-05-04 16:29:25
 */
@Slf4j
@Service
public class ForumPostServiceImpl extends ServiceImpl<ForumPostMapper, ForumPost> implements ForumPostService {
	@Autowired
	private ForumPostMapper postMapper;

	//TODO 依赖循环问题
	@Lazy
	@Autowired
	private ForumUserService forumUserService;

	@Override
	public String createPost(ForumPost post) {
		post.setUserId(StpUtil.getLoginIdAsLong());

		if (post.getImages() != null)
			post.setHasImage(PostHasImage.TRUE.getCode());

		// （0正常 1审核中 2驳回）
		boolean hasSensitive = SensitiveWordHelper.contains(post.getTitle())
				|| SensitiveWordHelper.contains(post.getContent());

		post.setStatus(hasSensitive ? 1 : 0);

		post.setCreateBy(SaUtils.getNickname());

		String ip = IpUtils.getIpAddr();
		post.setIpaddr(ip);
		post.setLocation(AddressUtils.getRealAddressByIP(ip));

		// TODO 发帖人信息没有++ ，删除可能也没有，点赞/收藏，也未能同步到个人信息
		if (save(post)){
			// 使用LambdaUpdateWrapper避免先查询再更新
			LambdaUpdateWrapper<ForumUser> updateWrapper =
					new LambdaUpdateWrapper<>();

			updateWrapper.eq(ForumUser::getUserId, post.getUserId())
					.setSql("post_count = post_count + 1");

			forumUserService.update(updateWrapper);
		}

		return hasSensitive
				? "包含敏感词，帖子已进入审核流程"
				: "发布成功！";
	}

	@Override
	public IPage<ForumPostListVo> getPostPageList(int pageNum, int pageSize, String query) {
		Long userId = null;
		if (StpUtil.getLoginIdDefaultNull() != null)
			userId = StpUtil.getLoginIdAsLong();

		Page<ForumPostListVo> page = new Page<>(pageNum, pageSize);
		return postMapper.selectPostPageWithLike(page, query, userId);
	}

	@Override
	public IPage<ForumPostListVo> getPostPageMy(int pageNum, int pageSize) {
		Long userId = StpUtil.getLoginIdAsLong();
		Page<ForumPostListVo> page = new Page<>(pageNum, pageSize);
		return postMapper.selectMyPostPageWithLike(page, userId);
	}

	@Override
	public IPage<ForumPostListVo> getPostPageMyCollect(int pageNum, int pageSize) {
		Long userId = StpUtil.getLoginIdAsLong();
		Page<ForumPostListVo> page = new Page<>(pageNum, pageSize);
		return postMapper.selectMyCollectPostPageWithLike(page, userId);
	}

	@Override
	public void incrementLikeCount(String postId) {
		update().setSql("like_count = like_count + 1")
				.eq("post_id", postId).update();
	}


	@Override
	public void decrementLikeCount(String postId) {
		update().setSql("like_count = like_count - 1")
				.eq("post_id", postId).update();
	}

	@Override
	public void incrementCollectCount(String postId) {
		update().setSql("collect_count = collect_count + 1")
				.eq("post_id", postId).update();
	}

	@Override
	public void decrementCollectCount(String postId) {
		update().setSql("collect_count = collect_count - 1")
				.eq("post_id", postId).update();
	}

	@Override
	public void incrementCommentCount(String postId) {
		update()
				.setSql("comment_count = comment_count + 1")
				.eq("post_id", postId)
				.update();
	}

	@Override
	public void decrementCommentCount(String postId) {
		update()
				.setSql("comment_count = comment_count - 1")
				.eq("post_id", postId)
				.update();
	}

	/**
	 * 查询论坛帖子列表
	 *
	 * @param forumPost 论坛帖子
	 * @return 论坛帖子
	 */
	@Override
	public List<ForumPostVo> selectForumPostList(ForumPost forumPost) {
		return postMapper.selectForumPostList(forumPost);
	}
}




